{% extends "custom_admin/base.html" %}

{% block title %}
{% if department %}编辑部门{% else %}创建部门{% endif %}
{% endblock %}

{% block content %}
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
    <h1 class="h2">{% if department %}编辑部门{% else %}创建部门{% endif %}</h1>
</div>

<div class="row">
    <div class="col-md-8">
        <form method="post">
            {% csrf_token %}
            
            <div class="mb-3">
                <label for="{{ form.name.id_for_label }}" class="form-label">部门名称</label>
                {{ form.name }}
                {% if form.name.errors %}
                <div class="text-danger">{{ form.name.errors }}</div>
                {% endif %}
            </div>
            
            <div class="mb-3">
                <label for="{{ form.code.id_for_label }}" class="form-label">部门编码</label>
                {{ form.code }}
                {% if form.code.errors %}
                <div class="text-danger">{{ form.code.errors }}</div>
                {% endif %}
            </div>
            
            <div class="mb-3">
                <label for="{{ form.description.id_for_label }}" class="form-label">描述</label>
                {{ form.description }}
                {% if form.description.errors %}
                <div class="text-danger">{{ form.description.errors }}</div>
                {% endif %}
            </div>
            
            <div class="mb-3">
                <label for="{{ form.parent.id_for_label }}" class="form-label">上级部门</label>
                {{ form.parent }}
                {% if form.parent.errors %}
                <div class="text-danger">{{ form.parent.errors }}</div>
                {% endif %}
            </div>
            
            {% if parent %}
            <div class="mb-3">
                <div class="alert alert-info">
                    当前创建的是 "{{ parent.name }}" 的子部门
                </div>
            </div>
            {% endif %}
            
            <div class="mb-3 form-check">
                {{ form.is_active }}
                <label for="{{ form.is_active.id_for_label }}" class="form-check-label">启用</label>
                {% if form.is_active.errors %}
                <div class="text-danger">{{ form.is_active.errors }}</div>
                {% endif %}
            </div>
            
            <div class="mb-3">
                <button type="submit" class="btn btn-primary">保存</button>
                <a href="{% url 'custom_admin:department_list' %}" class="btn btn-secondary">取消</a>
            </div>
        </form>
    </div>
</div>
{% endblock %}